package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.baidu.navisdk.util.common.StringUtils;
import com.iexbuy.ihg.App;
import com.iexbuy.ihg.R;
import com.iexbuy.ihg.adapter.YunGuoAttenceRecordsAdapter;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.common.ExtraKeyConstant;
import com.iexbuy.ihg.entity.Shop;
import com.iexbuy.ihg.entity.YunDealObject;
import com.iexbuy.ihg.entity.YunItemListObject;
import com.iexbuy.ihg.entity.YunLuckAttandUserListObject;
import com.iexbuy.ihg.entity.YunLuckAttandUserListObjectResponce;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.StringUtil;
import com.iexbuy.ihg.utils.ToastUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class YunGuoAttenceRecordsFragment extends BaseFragment {
	
	private static YunGuoAttenceRecordsFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.listView)
	private ListView listView;

	@ViewInject(R.id.productIcon)
	private ImageView productIcon;
	@ViewInject(R.id.productName)
	private TextView productName;
	@ViewInject(R.id.orderDetail)
	private TextView orderDetail;
	@ViewInject(R.id.buyNumber)
	private EditText buyNumber;

	private static YunItemListObject yunItemListObject;

	private List<YunLuckAttandUserListObject> YunLuckAttandUserListObjects = new ArrayList<YunLuckAttandUserListObject>();
	private int pageNo = 0;
	private YunGuoAttenceRecordsAdapter yunGuoAttenceRecordsAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_yunguo_attendance_records, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.yun_guo_attence_records));
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);

		initAttenceRecordsData();
	}


	public void initAttenceRecordsData() {

		if (yunItemListObject == null) return;

		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("itemId", yunItemListObject.getItemId());
		paramMap.put("period", yunItemListObject.getPeriod());
		paramMap.put("pageNo", String.valueOf(pageNo));
		paramMap.put("pageSize", String.valueOf(ExtraKeyConstant.COMMON_PAGE_SIZE));

		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_CHOU_JIAN_RECORDS, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
					}

					@Override
					public void onCancelled() {
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						YunLuckAttandUserListObjectResponce rep = JsonUtils.parseJsonStrToObject(responseInfo.result, YunLuckAttandUserListObjectResponce.class);
						if (pageNo == 0) {
							YunLuckAttandUserListObjects.clear();
						}
						YunLuckAttandUserListObjects.addAll(rep.getBody());

						initAttenceRecordsView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
					}
				});
	}

	private void initAttenceRecordsView() {
		if (yunGuoAttenceRecordsAdapter == null) {
			yunGuoAttenceRecordsAdapter = new YunGuoAttenceRecordsAdapter(mBaseActivity, YunLuckAttandUserListObjects);
			listView.setAdapter(yunGuoAttenceRecordsAdapter);
		}
		yunGuoAttenceRecordsAdapter.notifyDataSetChanged();
	}


	public static void setYunItemListObject(YunItemListObject yunItemListObject) {
		YunGuoAttenceRecordsFragment.yunItemListObject = yunItemListObject;
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static YunGuoAttenceRecordsFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
